package org.ysh.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.ysh.core.annotation.ResourceEx;
import org.ysh.core.utils.StringUtils;
import org.ysh.core.web.TableDataInfo;
import org.ysh.domain.*;
import org.ysh.service.DeptService;
import org.ysh.service.UserDeptService;
import org.ysh.vo.FondsTreeNodeVo;
import org.ysh.vo.FormSelectsVo;
import org.ysh.vo.R;
import org.ysh.vo.TreeNodeVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController{

    @Resource
    private DeptService deptService;

    @Resource
    private UserDeptService userDeptService;

    @RequiresPermissions("system:dept:list")
    @ResourceEx(name = "部门管理" , code = "system.dept",description = "部门管理",icon = "fa fa-sitemap",type = 0,sort = 1,parentCode = "system")
    @RequestMapping("/list")
    public String dept(){
        return "system/dept/list";
    }

    @RequiresPermissions("system:dept:list")
    @ResourceEx(name = "列表" , code = "system.dept.list",description = "部门管理-部门列表",icon = "fa fa-sitemap",type = 1,sort = 0,parentCode = "system",permission = "system:dept:list")
    @ResponseBody
    @GetMapping("/dept_list")
    public TableDataInfo getDeptList() {
        Department department = new Department();
        department.setFondsCode(getCurrentFondsCode());
        return getDataTable(deptService.findByPage(department));
    }

    @RequiresPermissions("system:dept:add")
    @ResourceEx(name = "新增" , code = "system.dept.add",description = "部门管理-新增部门",icon = "fa fa-minus-square",type = 1,sort = 1,permission = "system:dept:add" ,parentCode = "system.dept")
    @GetMapping("/add")
    public String add() {
        return "/system/dept/add";
    }

    @RequiresPermissions("system:dept:add")
    @ResponseBody
    @PostMapping("/add")
    public R<?> add(Department department) {
        Department queryDepartment = new Department(department.getDepartmentCode());
        queryDepartment.setFondsCode(getCurrentFondsCode());
        List<Department> departments = deptService.findByPage(queryDepartment);
        if(!departments.isEmpty()) {
            return R.fail("部门编码已存在!");
        }
        department.setFondsCode(getCurrentFondsCode());
        deptService.insert(department);
        return R.ok();
    }

    /**
     * 获取全宗树
     * @return
     */
    @RequiresPermissions("system:dept:list")
    @ResponseBody
    @GetMapping("/dept_tree")
    public List<TreeNodeVo> getFondsTree() {
        return deptService.getDeptTree(getCurrentFondsCode());
    }

    @RequiresPermissions("system:dept:edit")
    @ResourceEx(name = "编辑" , code = "system.dept.edit",description = "部门管理-编辑部门",icon = "fa fa-edit",type = 1,sort = 2,permission = "system:dept:edit" ,parentCode = "system.dept")
    @GetMapping("/edit")
    public ModelAndView edit(Integer id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("dept", deptService.findByPage(new Department(id)).get(0));
        mv.setViewName("/system/dept/edit");
        return mv;
    }

    @RequiresPermissions("system:dept:edit")
    @ResponseBody
    @PostMapping("/edit")
    public R<?> edit(Department dept) {
        if(dept.getId() == null){
            return R.fail("部门ID不能为空!");
        }
        deptService.update(dept);
        return R.ok();
    }

    @RequiresPermissions("system:dept:delete")
    @ResourceEx(name = "删除" , code = "system.dept.delete",description = "部门管理-删除部门",icon = "fa fa-minus-square",type = 1,sort = 3,permission = "system:dept:delete" ,parentCode = "system.dept")
    @ResponseBody
    @PostMapping("/delete")
    public R<?> delete(Integer id) {
        Department dept = new Department(id);
        dept.setDeleteBy(getCurrentUser().getDeleteBy());
        deptService.delete(dept);
        return R.ok();
    }


    /**
     * 部门下拉列表
     * @param value 用户id
     * @return
     */
    @RequiresPermissions(value = "system:dept:list")
    @GetMapping("/dept_selects")
    @ResponseBody
    public R<List<FormSelectsVo>> getUserSelects(String value) {
        //查询所有部门信息
        List<Department> departments = deptService.findByPage(new Department());
        List<Integer> deptIds = new ArrayList<>();
        if(StringUtils.isNotEmpty(value))  {
            //查询当前用户的部门
            deptIds.addAll(userDeptService.findDeptIdByUserId(Integer.parseInt(value)));
        }
        return R.ok(userDeptService.treeify(departments,deptIds));
    }



}
